<?php

namespace App\Http\Controllers;

use App\Utils\ResponseTrait;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use ResponseTrait;

    private $filter = null;

    /**
     * 获取必须参数
     * @param Request $request
     * @param mixed ...$name
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function required(Request $request, ...$name): array {
        $rules = [];
        foreach ($name as $v) {
            $rules[$v] = 'required';
        }
        return $this->validate($request, $rules);
    }

    /**
     * 分页
     * @return array
     */
    protected function page()
    {
        return [($_GET['page'] ?? 1) ?: 1, ($_GET['size'] ?? 10) ?: 10];
    }

    protected function filter(string $name)
    {
        if ($this->filter === null) {
            $filter = $_GET['filter'] ?? null;
            $this->filter = $filter ? json_decode($filter, true) : [];
        }
        return $this->filter[$name] ?? null;
    }
}
